# coding=utf-8
from playwright.sync_api import sync_playwright
import time
import random
import pickle
import json
import pymysql
from parsel import Selector
from pymysql.converters import escape_string
# 查询专利代理机构2021-2022代理专利详细信息，主要使用playwright实现
# playwright录制脚本命令： playwright codegen -o scripy.py

conn = pymysql.connect(host="localhost",
                       user='root',
                       port=3306,
                       password='123456',
                       database='big_data')
cur = conn.cursor()

# word = r'内分泌'
# word = r'整形外科'
# word = r'肿瘤'
# word = r'病理'
# word = '疼痛'
# word ='老年'
# word='耳鼻喉'
# word='肾内'
# word = '更昔洛韦'
# -----------------
# 未抓
# word = '伐昔洛韦'
# word = '细胞形态识别'
# word = '教学平台'
# word = '会诊平台'
# word = '智慧学习平台'

num_limit = 100


def parse_page(page_json):
    # 解析每页专利详细信息
    patent_info_list = page_json.get('t').get('searchResultRecord')
    if patent_info_list:
        for patent_info in patent_info_list:
            # try:
            fm_name = patent_info.get('ti')
            fm_name = escape_string(fm_name)
            items = patent_info.get('items').get('COM')
            sqh = ''.join([i.get('value')
                           for i in items if i.get('indexEnName') == 'APO'])
            sq_date = patent_info.get('apd')
            if isinstance(sq_date, str):
                sq_date = sq_date.replace('.', '-')
            dljg_str = ''.join([i.get('value')
                                for i in items if i.get('indexEnName') == 'AGY'])
            dljg_name = ''
            dljg_code = ''
            if isinstance(dljg_str, str):
                dljg_list = dljg_str.split(' ', 1)
                if len(dljg_list) > 1:
                    dljg_name = dljg_str.split(' ')[0]
                    dljg_code = dljg_str.split(' ')[1]
            else:
                dljg_name = ''
                dljg_code = ''
            fmr = ''.join([i.get('value')
                           for i in items if i.get('indexEnName') == 'INVIEW'])
            fmr = escape_string(fmr)
            sqr = ''.join([i.get('value')
                           for i in items if i.get('indexEnName') == 'PAVIEW'])
            sqr = escape_string(sqr)
            # gk_items = patent_info.get('items').get('GK')
            gk_date = patent_info.get('pd')
            if isinstance(gk_date, str):
                gk_date = gk_date.replace('.', '-')
            gkh = patent_info.get('pnId')
            ipc_num = ''.join([i.get('value') for i in patent_info.get(
                'ipcDetail') if i.get('indexEnName') == 'GK_ICST'])
            ipc_main = patent_info.get('ipcMain')
            cpc_fujia = ''.join([i.get('value') for i in patent_info.get(
                'cpcDetail') if i.get('indexId') == 'CNDB122'])
            cpc_famin = ''.join([i.get('value') for i in patent_info.get(
                'cpcDetail') if i.get('indexId') == 'CNDB123'])
            sq_abstract_text = ''.join([i.get('value') for i in patent_info.get(
                'abview') if i.get('indexId') == 'CNDB078'])
            ab_soup = Selector(sq_abstract_text)
            absstrct = ab_soup.xpath('//text()').extract_first()  # 摘要
            if absstrct:
                absstrct = absstrct.replace('\n', '').replace(
                    ' ', '').replace('\r', '')
                absstrct = escape_string(absstrct)
            else:
                absstrct = ''
            # 专利详情插入数据库
            insert_sql = f"""INSERT INTO word_patent_list (apply_number,apply_date,agent_name,agent_code,sq_gonkaihao,sq_gonkaidate,sqr_name,sq_faminren,sq_title,ipc_num,ipc_main,cpc_fujia,cpc_famin,absstrct,search_word) VALUES('{sqh}','{sq_date}','{dljg_name}','{dljg_code}','{gkh}','{gk_date}','{sqr}','{fmr}','{fm_name}','{ipc_num}','{ipc_main}','{cpc_fujia}','{cpc_famin}','{absstrct}','{word}')"""
            # print(insert_sql)
            cur.execute(insert_sql)
            conn.commit()
            # except:
            #     continue


def request_page_list(context, page, authorization, executableSearchExp, searchExp, page_num):
   # 生成搜索词加密串
    from update_pss_auth_info import get_key, jiami_words
    key, encrypted_key = get_key()
    # print([key, encrypted_key])
    jiami_exeSearchExp = jiami_words(executableSearchExp, key)
    jiami_searchExp = jiami_words(searchExp, key)
    # print(jiami_exeSearchExp)
    # print(jiami_searchExp)

    # 发送请求获取每页专利明细
    api_request_context = context.request
    headers = {
        "Accept": "application/json, text/plain, */*",
        "Content-Type": "application/json;charset=utf-8",
        "Authorization": 'Bearer ' + authorization,
        "language": "zh",
        "EncryptAesKey": encrypted_key
    }
    data = {
        "executableSearchExp": jiami_exeSearchExp,
        "parseSearchExp": jiami_exeSearchExp,
        "searchExp": jiami_searchExp,
        "sortFields": "-APD",
        "pagination": {"page": page_num, "limit": num_limit},
        "dbName": "CNDB%2CWPDB",
        "interfaceId": "2001154"}
    page_response = api_request_context.post(
        'https://pss-system.cponline.cnipa.gov.cn/api/pubsearch-app-search/search/results/getResults', data=data, headers=headers, timeout=0)
    try:
        page_json = page_response.json()
    except:
        print('数据请求失败，10秒后重试')
        time.sleep(10)
        page.reload()
        request_page_list(context, page, authorization,
                          jiami_exeSearchExp, jiami_searchExp, page_num)
    status = page_json.get('status')
    if status != 200:
        print(page_json)
    print(f'---pagenum:{page_num}_status:{status}---')
    return(page_json)


def run_main(executableSearchExp, searchExp):
    with sync_playwright() as p:
        storage_state_path = './auth/XHHG_pss_state.json'
        authorization = ''
        with open(storage_state_path) as file:
            storage_state = json.load(file)
            origins = storage_state.get('origins')
            local_stroge_list = [item.get('localStorage') for item in origins]
            for local_stroge in local_stroge_list:
                for item in local_stroge:
                    if item.get('name') == 'token':
                        authorization = item.get('value')
        print(f'成功读取pss网站XHHG登录信息:{authorization}')
        browser = p.firefox.launch()
        context = browser.new_context(
            base_url='https://pss-system.cponline.cnipa.gov.cn/api', storage_state=storage_state_path)
        context.set_default_timeout(600000)
        context.set_default_navigation_timeout(600000)
        page = context.new_page()
        try:
            page.goto(
                'https://pss-system.cponline.cnipa.gov.cn/conventionalSearch')
            page.wait_for_load_state()
            page.goto('https://pss-system.cponline.cnipa.gov.cn/seniorSearch')
            page.wait_for_load_state()
        except:
            print('page_load_error')
            page.wait_for_timeout(5000)
        if authorization:
            # 请求首页信息获取全部条数
            # print(f'@@@开始解析：{searchExp}_第1页数据@@@')
            # firstpage_json = request_page_list(
            #     context, page, authorization, executableSearchExp, searchExp, 1)
            # total_count = firstpage_json.get('t').get(
            #     'pagination').get('totalCount')
            # print(f'{word}_total_count:{total_count}')
            # # 解析最大页数
            # if total_count:
            #     patent_number = int(total_count)
            # else:
            #     total_count = 0
            # import math
            # max_page_number = math.ceil(total_count / num_limit)
            # print(
            #     f'@@@@@@@@@@ max_page_number:{max_page_number}---@@@@@@@@@')
            # if word not in ('疼痛'):
            #     parse_page(firstpage_json)
            # page.wait_for_timeout(5000)
            # print(f'======{searchExp}_第1页数据解析完成=====')
            max_page_number = 555
            if max_page_number > 1:
                # 解析剩下页面数据
                if word == '疼痛':
                    start_page = 114
                else:
                    start_page = 2
                for page_num in range(start_page, max_page_number + 1):
                    page.reload()
                    page.wait_for_load_state()
                    print(f'@@@开始解析：{searchExp}_第{page_num}页数据@@@')
                    page_json = request_page_list(
                        context, page, authorization, executableSearchExp, searchExp, page_num)
                    parse_page(page_json)
                    print(f'======{searchExp}_第{page_num}页数据解析完成=====')
                    if page_num % 5 == 0:
                        page.wait_for_timeout(random.randint(5, 10) * 1000)


if __name__ == '__main__':
    # pa_list = ['孝感学院']
    # for pa in pa_list:
    #     executableSearchExp = f"(PA='{pa}')"
    #     searchExp = f"申请（专利权）人=({pa})"
    #     print(f'searchExp:{searchExp}')
    #     run_main(executableSearchExp, searchExp)

    # from update_pss_auth_info import update_pss_auth
    # update_pss_auth('XHHG')
    # print('@@已更新登录信息@@')

    # searchExp = f'关键词=({word})  OR 说明书=({word})  OR 权利要求=({word})  OR 摘要=({word})  OR 发明名称=({word})'
    # executableSearchExp = f"((BA='{word}' OR DESC='{word}' OR CLMS='{word}' OR ABS='{word}' OR TI='{word}')) AND CC =('CN')"
    # run_main(executableSearchExp, searchExp)

    # seg_word_list = ['口腔清洁器','呼吸口罩','呼吸口罩','锻炼床','营养液调配','营养液喂送','输液架','身体清洗','定位穿刺','气道湿化','止血装置','麻醉气体净化','呼吸训练','引流袋','CT 定位','手术观察灯','止血装置','器械 消毒装置','听诊装置','造影注射器','呼吸训练','配药 碾碎 ','药膏涂抹','样本震荡','验尿杯托架','止鼾器','咽喉镜','压舌器','咽喉扩张','标本保存','甲状腺剥离器','样本染色','组织切片','肾结石摘除','导尿器','活检钳','血管夹','管线架','肢体固定','样本摇晃','血液透析','肿瘤夹取','肿瘤取样','肿瘤 刀','肿瘤测量','手术拉钩','加压包扎','止血装置','头发固定','颌面矫正','面部轮廓','注意力训练','起夜报警','手部锻炼','坐浴装置','生殖保护','坐便器','取卵装置','毛发剔除','皮肤硬度','穴位按摩','热疗垫']
    # sql = f'SELECT DISTINCT search_word FROM chachon_word_patent_list'
    # cur.execute(sql)
    # data = cur.fetchall()
    # have_already_done = set([sqh[0] for sqh in data])
    # print('@@ 查询已抓取关键词 @@')
    seg_word_list = ['疼痛']
    for word in seg_word_list:
        # if word not in have_already_done:
        word_list = word.split(' ')
        searchExp = ' AND '.join([f'关键词=({word})' for word in word_list])
        executableSearchExp = ' AND '.join(
            [f"BA=('{word}')" for word in word_list])
        executableSearchExp = f'({executableSearchExp})' + " AND CC =('CN')"
        run_main(executableSearchExp, searchExp)
    conn.close()
